<!--
 * @Author: 梁卓
 * @Date: 2022-02-01 21:54:54
 * @LastEditTime: 2022-02-07 23:23:40
 * @LastEditors: Please set LastEditors
 * @Description: 权限管理界面
 * @FilePath: \dreamUi\src\views\system\Permission.vue
-->
<template>
  <div>
    <el-card>
        <el-row>
          <el-col :span="8">
            <el-input  v-model="queryInfo.queryString" clearable placeholder="Please input" @clear="findPermission">
              <el-button icon="el-icon-search" slot="append" @click="findPermission"></el-button>
            </el-input>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" @click="insert" v-hasPermission="['PRE_INSERT']">添加权限</el-button>
          </el-col>
      </el-row>
      <el-table  v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"  />
        <el-table-column prop="label" label="权限标签" />
        <el-table-column prop="code" label="权限标签值" />
        <el-table-column  fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="small" v-hasPermission="['PRE_UPDATE']" @click="updatePermission(scope)" />
            <el-button type="danger" icon="el-icon-delete" circle size="small" v-hasPermission="['PRE_DELETE']"  @click="deletePermission(scope)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="total > 0"
        :currentPage="queryInfo.currentPage"
        :page-sizes="[8,15,30,50]"
        :page-size= "queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total= total
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>
    <!-- 权限的添加和修改 -->
    <el-dialog :title="title" :visible.sync="open" width="35%" @closed="dialogClose">
      <el-form :model="form" ref="form" :rules="rule">
        <el-form-item label="权限标签" prop="label">
          <el-input v-model="form.label" />
        </el-form-item>
        <el-form-item label="权限标签值" prop="code">
          <el-input v-model="form.code"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFrom">取 消</el-button>
        <el-button type="primary" @click="insertPermission">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Permission',

  data() {
    return {
      // 分页
      queryInfo: {
        pageNumber: 1,
        pageSize: 8,
        queryString: ''
      },
      // 表格数据
      tableData: [],
      // 总条数
      total: 0,
      loading: true,
      // 添加或删除的标题
      title: '',
      // 是否打开对话框
      open: false,
      // 表单数据
      form: {
        label: '',
        code: ''
      },
      // 表单校验
      rule: {
        label: [
          {required: true, message: '请输入权限标签', trigger: 'blur'},
          {min: 1, max: 50, message: '权限标签的长度为1~50之间', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入权限标签值', trigger: 'blur'},
          {min: 1, max: 20, message: '权限标签的长度为1~20之间', trigger: 'blur'}
        ]
      }
    };
  },

  mounted() {
    
  },
  created() {
    this.findPermission();
  },
  methods: {
    // 查询分页数据
    findPermission() {
      this.$ajax.post('/permission/findPage',this.queryInfo).then((res) => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.loading = false
      });
    },
    // 改变页码
    handleSizeChange(param) {
      this.queryInfo.pageSize = param;
      // 重新赋值
      this.findPermission();
    },
    // 改变当前的页面
    handleCurrentChange(param) {
      this.queryInfo.pageNumber = param;
      this.findPermission();
    },
    // 添加按钮事件
    insert(param) {
      this.title = '新增权限信息';
      this.open = true;
    },
    // 删除权限信息
    deletePermission(param) {
      this.$confirm(`您将永久删除-[${param.row.label}]-的权限, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$ajax.delete(`/permission/delete/${param.row.id}`).then((res) => {
            this.$message.success(res.data.message);
            // 防止删除最后一页中最后一个数据时无法换页的问题
            this.queryInfo.pageNumber = 1;
            // 删除成功后更新数据
            this.findPermission();
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 关闭对话框事件
    dialogClose() {
      // 将整个表单进行重置
      this.$refs.form.resetFields();
    },
    // 点击取消对话框
    closeFrom() { 
      this.form = {},
      this.open = false
      this.findPermission()
    },
    // 修改权限信息
    updatePermission(param) {
      this.form = param.row;
      this.title = '修改权限信息';
      this.open = true;
    },
    // 添加权限
    insertPermission(param) {
      this.$refs.form.validate(val => {
        // 校验不同通过
        if (!val) {
          return this.$message.error('表单校验不通过，请检查后提交')
        }
        // 判断是否时新增
        if (this.form.id === undefined || this.form.id === null) {
          this.$ajax.post('/permission/insert',this.form).then(res => {
            this.$message.success(res.data.message);
            this.open = false;
            this.findPermission();
          });
        } else {
          this.$ajax.put('/permission/update',this.form).then(res => {
            this.$message.success(res.data.message);
            this.open = false;
            this.findPermission();
          });
        }
      });
    }
  },
};
</script>

<style scoped>

</style>
